#include "database.h"

QSqlDatabase g_MCDataBase;

QVariant get_mc_info(QString groupname, QString keyname)
{
    QSettings mcsetting(MC_UI_CONFIG_FILE , QSettings::IniFormat);
    mcsetting.setIniCodec(QTextCodec::codecForName("UTF-8"));

//    mcsetting.beginGroup("SQliteInfo]");
//    mcsetting.setValue("dbfile" , "/home/i2dsp-chengdu/AdrianProgram/build-i2DSP_V0-Desktop_Qt_5_9_1_GCC_64bit-Debug/i2DSPLocalServer/i2dspSD.db");
//    mcsetting.endGroup();

//    mcsetting.beginGroup("[MCUIInfo]");
//    mcsetting.setValue("mcid" , "MC0000001");
//    mcsetting.setValue("name" , "神山工业城精恒电子茂强办公楼");
//    mcsetting.setValue("lat" , 114.123456);
//    mcsetting.setValue("lng" , 114.654321);
//    mcsetting.setValue("baidumaphtml" , "file:///home/i2dsp-chengdu/AdrianProgram/东莞地图/baidumapoffline/demo.html");
//    mcsetting.endGroup();

//    mcsetting.beginGroup("[SHM]");
//    mcsetting.setValue("shmonekey" , "SHMSDOne");
//    mcsetting.setValue("shmtwokey" , "SHMSDTwo");
//    mcsetting.endGroup();

    QVariant keyValue;
    mcsetting.beginGroup(groupname);
    keyValue = mcsetting.value(keyname);
    mcsetting.endGroup();

    if(keyValue.isNull()){
        qDebug()<<FLINE<<"Get Mc Info :"<<groupname<<keyname<<keyValue<<" Error";
    }
    return keyValue;
}
bool connect_database()
{
    g_MCDataBase = QSqlDatabase::addDatabase("QSQLITE" , "mcconnection");
    g_MCDataBase.setDatabaseName(get_mc_info("[SQliteInfo]" , "dbfile").toString());
    if(g_MCDataBase.open()){
        qDebug()<<FLINE<<g_MCDataBase.databaseName()<<" Open Successed";
        return true;
    }
    qDebug()<<FLINE<<g_MCDataBase.databaseName()<<" Open Failed";
    return false;
}
bool exec_database(QString statement)
{
    if(statement.isEmpty())
        return false;
    QSqlQuery  query(g_MCDataBase);
    if(!query.exec(statement)){
        qDebug()<<FLINE<<"Sql Query Error ??"<<statement<<" Error:"<<query.lastError();
        return false;
    }
    return true;
}
bool query_database(QString statement, QSqlQuery &query)
{
    if(statement.isEmpty() || &query == NULL)
        return false;
    QSqlQuery  Query(g_MCDataBase);
    if(!Query.exec(statement)){
        if(!Query.isSelect() || !Query.isValid()){
            qDebug()<<FLINE<<"Sql Query ??"<<statement<<" Error:"<<Query.lastError();
            query.clear();
            return false;
        }
    }
    query = Query;
    return true;
}
bool get_floor_devices(int devtype, int buildno, int floorno , QSqlQuery &query)
{
    QString strQuery;
    if(devtype == DEVICE_LT){
         strQuery = QString("selcet ltid,xpos,ypos,online from lt where buildingnum=%1 and floornum=%2")
                .arg(buildno).arg(floorno);
    }
    else if(devtype == DEVICE_SD100){
         strQuery = QString("selcet id,ltid,port,xpos,ypos,online,emglv,power from sensor where buildingnum=%1 and floornum=%2")
                .arg(buildno).arg(floorno);
    }
    else if(devtype == DEVICE_SPEAKER){
         strQuery = QString("selcet id,ltid,port,xpos,ypos,online,emglv,power from sensor where buildingnum=%1 and floornum=%2")
                .arg(buildno).arg(floorno);
    }
    QSqlQuery myquery(g_MCDataBase);
    if(!myquery.exec(strQuery)){
        query.clear();
        return false;
    }
    else{
        query = myquery;
        return true;
    }
}
void init_test_database()
{
    //lt
    QSqlQuery ltQuery(g_MCDataBase);
    int ltno = 31;
    QString sdid = "SDK0000000"; int sdno = 1;
    QString ltid = "LTK0000000" ; QString mcid = "MC0000000001"; int buildnum = 9; int floor = 1; int xpos = 10; int ypos = 10;
    QString addr = "test addr";
    for(int i = 0 ; i < 2 ; i++){
        if(i == 1){
            floor = 2;
        }
        ltid = "LTK0000000"+QString::number(ltno);
        QString strQuery = QString("insert into lt(ltid,mcid,buildingnum,floornum,xpos,ypos,addr,online) values('%1','%2',%3,%4,%5,%6,'%7',%8)")
                .arg(ltid).arg(mcid).arg(buildnum).arg(floor)
                .arg(xpos).arg(ypos).arg(addr).arg(0);

        if(!ltQuery.exec(strQuery)){
            qDebug()<<"Insert test lt error";
        }

        ltno++;
        xpos+=30;
        ypos+=30;


        for(int j = 1 ; j < 16 ; j++){
            QString strsdQuery = QString("insert into sensor(id,ltid,port,buildingnum,floornum,xpos,ypos,addr,type,emglv,online,power,emgno,emgtime) "
                                         "values('%1','%2',%3,%4,%5,%6,%7,'%8',%9,%10,%11,%12,%13,'%14')")
                    .arg(sdid+QString().sprintf("%02d",sdno)).arg(ltid).arg(j).arg(buildnum).arg(floor)
                    .arg(xpos).arg(ypos).arg(addr)
                    .arg(2).arg(0).arg(0).arg(0).arg(0).arg("");

            sdno++;
            xpos+=30;
            ypos+=30;

            if(!ltQuery.exec(strsdQuery)){
                qDebug()<<"Insert test sd error";
            }
        }
        xpos=30;
        ypos=30;

    }


}

void delete_database()
{
    QString strQuery = "delete from lt";
    QSqlQuery query(g_MCDataBase);
    if(!query.exec(strQuery))
        qDebug()<<"database clear error";
    strQuery = "delete from sensor";
    if(!query.exec(strQuery))
        qDebug()<<"database clear error";
}










